//
//  RoomScene.m
//  PokerPlus
//
//  Created by sunrui-MacBook on 11-7-29.
//  Copyright 2011 139.ME. All rights reserved.
//

#import "RoomScene.h"
#import <QuartzCore/QuartzCore.h>

@implementation Room

+(id) scene
{
	// 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	Room *layer = [Room node];
	
	// add layer as a child to scene
	[scene addChild: layer];
	
	// return the scene
	return scene;
}

-(id) init
{
	if( (self=[super init] )) {
		self.isTouchEnabled = YES;
		//添加背景
		CGSize size = [[CCDirector sharedDirector] winSize];
		
		CCSprite * background;
		if (FBIsDeviceIPad()) {
			background = [CCSprite spriteWithFile:@"RoomBackground_iPad.png"];

		}
		else {
			background =[CCSprite spriteWithFile:@"RoomBackground_iPhone.png"];
		}
		
		background.position = ccp(size.width/2,size.height/2);	
		
		[self addChild: background];
		
		_table = [Table node];
		[self addChild:_table];

		if (FBIsDeviceIPad()) {
			poker = [CCMenuItemImage itemFromNormalImage:@"牌型组合.png" selectedImage:@"牌型组合.png" disabledImage:@"牌型组合.png" target:self selector:@selector(pokermove:)];
			backHall = [CCMenuItemImage itemFromNormalImage:@"前往大厅能点.png" selectedImage:@"前往大厅不能点.png" disabledImage:@"前往大厅不能点.png" target:self selector:@selector(backHallmove:)];
			standup = [CCMenuItemImage itemFromNormalImage:@"起身能点中.png" selectedImage:@"起身不能点中.png" disabledImage:@"起身不能点中.png" target:self selector:@selector(standupmove:)];
			
			fold = [CCMenuItemImage itemFromNormalImage:@"盖牌能点.png" selectedImage:@"盖牌不能点.png" disabledImage:@"盖牌不能点.png" target:self selector:@selector(standupmove:)];
			check = [CCMenuItemImage itemFromNormalImage:@"过牌能点.png" selectedImage:@"过牌不能点.png" disabledImage:@"过牌不能点.png" target:self selector:@selector(standupmove:)];
			follow = [CCMenuItemImage itemFromNormalImage:@"跟注能点.png" selectedImage:@"跟注不能点.png" disabledImage:@"跟注不能点.png" target:self selector:@selector(standupmove:)];
			add = [CCMenuItemImage itemFromNormalImage:@"加注能点.png" selectedImage:@"加注不能点.png" disabledImage:@"加注不能点.png" target:self selector:@selector(standupmove:)];
			foldcheck = [CCMenuItemImage itemFromNormalImage:@"过牌盖牌能点.png" selectedImage:@"过牌盖牌不能点.png" disabledImage:@"过牌盖牌不能点.png" target:self selector:@selector(standupmove:)];
			followany = [CCMenuItemImage itemFromNormalImage:@"跟任意注能点.png" selectedImage:@"跟任意注不能点.png" disabledImage:@"跟任意注不能点.png" target:self selector:@selector(standupmove:)];
			chat = [CCMenuItemImage itemFromNormalImage:@"聊天能点.png" selectedImage:@"聊天能点.png" disabledImage:@"聊天能点.png" target:self selector:@selector(standupmove:)];
			
			CCMenu * pokermenu = [CCMenu menuWithItems:poker,nil];
			CCMenu * backHallmenu = [CCMenu menuWithItems:backHall,nil];
			CCMenu * standupmenu = [CCMenu menuWithItems:standup,nil];
			
			CCMenu * foldmenu = [CCMenu menuWithItems:fold,nil];
			CCMenu * checkmenu = [CCMenu menuWithItems:check,nil];
			CCMenu * followmenu = [CCMenu menuWithItems:follow,nil];
			CCMenu * addmenu = [CCMenu menuWithItems:add,nil];
			CCMenu * foldcheckmenu = [CCMenu menuWithItems:foldcheck,nil];
			CCMenu * followanymenu = [CCMenu menuWithItems:followany,nil];
			CCMenu * chatmenu = [CCMenu menuWithItems:chat,nil];
			
			pokermenu.position = ccp(-115,size.height/2);
			backHallmenu.position = ccp(950,600);
			standupmenu.position = ccp(950,550);
			
			foldmenu.position = ccp(450,100-20);
			checkmenu.position = ccp(575,100-20);
			followmenu.position = ccp(700,100-20);
			addmenu.position = ccp(825,100-20);
			
			foldcheckmenu.position = ccp(513,100-65);
			followanymenu.position = ccp(763,100-65);
			chatmenu.position = ccp(953,100-65);
			
			[self addChild:pokermenu z:999];
			[self addChild:backHallmenu z:999];
			[self addChild:standupmenu z:999];
			[self addChild:foldmenu z:999];
			[self addChild:checkmenu z:999];
			[self addChild:followmenu z:999];
			[self addChild:addmenu z:999];
			[self addChild:foldcheckmenu z:999];
			[self addChild:followanymenu z:999];
			[self addChild:chatmenu z:999];
		}
		_textViewImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"对话框.png"]];
		_textView = [[UITextView alloc] initWithFrame:CGRectMake(3, 768-100, 377, 93)];
		[_textView addSubview:_textViewImage];
		[_textView sendSubviewToBack:_textViewImage];
		[_textView setBackgroundColor:[UIColor clearColor]];
		_textView.layer.cornerRadius = 8.0f;
		_textView.alpha = 0.0f;
		_textView.delegate = self;
		_textView.returnKeyType = UIReturnKeySend;
		
		[[[AppDelegate sharedDelegate] rootView] addSubview:_textView];
		
		[UIView animateWithDuration:0.5 delay:0.4 options:UIViewAnimationOptionCurveEaseInOut animations:^{
			_textView.alpha = 1.0f;
		} completion:^(BOOL finished){
			
		}];
		
		//[[[AppDelegate sharedDelegate] rootView] performSelector:@selector(addSubview:) withObject:_textView afterDelay:0.2f];
		[[NSNotificationCenter defaultCenter] addObserver:_textView selector:@selector(resignFirstResponder) name:@"EnterBackground" object:nil];
	}

	return self;
}

- (void)pokermove:(CCMenuItemImage *)sender{
	CGSize size = [[CCDirector sharedDirector] winSize];
	if (sender.parent.position.x < 0) {
		//sender.position = ccp(135,768/2);
		[sender.parent runAction:[CCMoveTo actionWithDuration:0.5f position:ccp(145,size.height/2)]];
	}
	else {
		//sender.position = ccp(-135,768/2);
		[sender.parent runAction:[CCMoveTo actionWithDuration:0.5f position:ccp(-115,size.height/2)]];
	}

}

- (void)backHallmove:(CCMenuItemImage *)sender{
	[_textView resignFirstResponder];
	[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
		_textView.alpha = 0.0f;
	} completion:^(BOOL finished){
		
		[_textViewImage removeFromSuperview];
		[_textView removeFromSuperview];
	}];

	
	[_table removeTable];
	[[AppDelegate sharedDelegate] ServerConnected:[[CCDirector sharedDirector] openGLView]];
	[[CCDirector sharedDirector] performSelector:@selector(end) withObject:nil afterDelay:1.0f];
	
}

- (void)standupmove:(CCMenuItemImage *)sender{
	
}


- (void)dealloc{
	
	[_textViewImage release];
	
	[_textView release];
	[self removeAllChildrenWithCleanup:YES];
	
	[super dealloc];
	
}

-(void) registerWithTouchDispatcher { 
	[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}

-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
	NSLog(@"roomscene touch");
	[_textView resignFirstResponder];
	if (poker.parent.position.x >0) {
		[self pokermove:poker];
	}
	//[self textViewShouldEndEditing:_textView];
	
	
	return YES;
}


#pragma mark -

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
	if (poker.parent.position.x >0) {
		[self pokermove:poker];
	}
	CGSize size = [[CCDirector sharedDirector] winSize];
	[_table runAction:[CCSpawn actions: 
					   [CCMoveTo actionWithDuration:0.3 position:ccp(size.width/4,130)],
					   [CCScaleTo actionWithDuration:0.3 scale:0.6f]
					   ,nil]];
	
	
	[_textViewImage setContentMode:UIViewContentModeScaleToFill];
	_textViewImage.image = [UIImage imageNamed:@"大对话框.png"];
	
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:0.4];
	_textViewImage.frame = CGRectMake(0, 0, 459, 300);
	_textView.frame = CGRectMake(3, 107, 459,300);
	[UIView commitAnimations];
	
	return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
	
	
	[_table runAction:[CCSpawn actions: 
					   [CCMoveTo actionWithDuration:0.3 position:ccp(0,0)],
					   [CCScaleTo actionWithDuration:0.3 scale:1.0f]
					   ,nil]];
	
	
	[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
		_textView.frame = CGRectMake(3, 768-100, 377, 93);
	} completion:^(BOOL finished){
		
		_textViewImage.frame = CGRectMake(0, 0, 377, 93);
		//[_textViewImage setContentMode:UIViewContentModeScaleToFill];
		_textViewImage.image = [UIImage imageNamed:@"对话框.png"];
	}];
	
	return YES;
}


@end
